/**
 *  Copyright (c) 1997-2013, www.tinygroup.org (luo_guo@icloud.com).
 *
 *  Licensed under the GPL, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.gnu.org/licenses/gpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.hundsun.finance.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.apache.commons.lang.StringEscapeUtils;
import org.tinygroup.beancontainer.BeanContainer;
import org.tinygroup.beancontainer.BeanContainerFactory;
import org.tinygroup.tinyscript.tree.impl.DataNodeUtil;
import org.tinygroup.tinyscript.tree.impl.TreeDataNode;

//@Controller
//@RequestMapping(produces="application/json;charset=UTF-8")
public class CommonController{
	
//	@Autowired 
//	private com.hundsun.finance.service.HelloService helloService;
//	
//	@Autowired 
//	private com.hundsun.finance.service.StockService stockService;
//	
//	@Autowired 
//	private com.hundsun.finance.service.SaleService saleService;
//	
//    @RequestMapping(value="/helloworld",method=RequestMethod.GET)
//	@ResponseBody
//	public String helloworld(@RequestParam(required=false) String name){
//		try {
//			
//			testRegisiterBean();
//			TreeDataNode dataNode = helloService.helloworld(name);
//			return DataNodeUtil.toJson(dataNode);
//		} catch (Exception e) {
//			  return String.format("{code:500,msg:\"%s\"}", StringEscapeUtils.escapeJavaScript(e.getMessage()));
//		}
//	}
//    
//    protected void testRegisiterBean(){
//    	BeanContainer<?>  factory =  BeanContainerFactory.getBeanContainer(getClass().getClassLoader());
//		System.out.println("factory="+factory.getBeanContainerPrototype().getClass().getName());
//		AbstractApplicationContext context = (AbstractApplicationContext) factory.getBeanContainerPrototype();
//		DefaultListableBeanFactory beanFactory =  (DefaultListableBeanFactory)context.getBeanFactory();
//		
//		GenericBeanDefinition  beanDefinition = new GenericBeanDefinition();
//		beanDefinition.setBeanClassName("com.hundsun.finance.controller.DynamicHello");
//		beanDefinition.setScope(BeanDefinition.SCOPE_SINGLETON);
//		beanDefinition.setLazyInit(false);
//		beanDefinition.setAutowireCandidate(true);
//		beanFactory.registerBeanDefinition("dynamicHello", beanDefinition);
//		
//		System.out.println("hello="+factory.getBean("dynamicHello"));
//    }
//    
//    @RequestMapping(value="/uploadstock",produces="multipart/form-data;charset=UTF-8",method=RequestMethod.POST)
//	@ResponseBody
//	public String insertStocks(@RequestParam String encode,@RequestParam MultipartFile file){
//		try {
//			TreeDataNode dataNode = stockService.insertStocks(file.getInputStream(), encode);
//			return DataNodeUtil.toJson(dataNode);
//		} catch (Exception e) {
//			return String.format("{code:500,msg:\"%s\"}", StringEscapeUtils.escapeJavaScript(e.getMessage()));
//		}
//	}
//    
//    @RequestMapping(value="/stock",method=RequestMethod.GET)
//	@ResponseBody
//	public String getStocks(@RequestParam(required=false) String startDT,@RequestParam(required=false) String endDT){
//		try {
//			TreeDataNode dataNode = stockService.getStocks(startDT, endDT);
//			return DataNodeUtil.toJson(dataNode);
//		} catch (Exception e) {
//			  return String.format("{code:500,msg:\"%s\"}", StringEscapeUtils.escapeJavaScript(e.getMessage()));
//		}
//	}
//    
//    @RequestMapping(value="/countperiod",method=RequestMethod.GET)
//	@ResponseBody
//	public String countPeriod(@RequestParam String period){
//		try {
//			TreeDataNode dataNode = stockService.countPeriod(period);
//			return DataNodeUtil.toJson(dataNode);
//		} catch (Exception e) {
//			  return String.format("{code:500,msg:\"%s\"}", StringEscapeUtils.escapeJavaScript(e.getMessage()));
//		}
//	}
//    
//    @RequestMapping(value="/countdays",method=RequestMethod.GET)
//	@ResponseBody
//	public String countDays(@RequestParam(required=false) String startDT,@RequestParam(required=false) String endDT,@RequestParam Double riseMax,@RequestParam Double riseMin
//			,@RequestParam Double fallMin,@RequestParam Double fallMax){
//		try {
//			TreeDataNode dataNode = stockService.countDays(startDT, endDT, riseMax, riseMin, fallMin, fallMax);
//			return DataNodeUtil.toJson(dataNode);
//		} catch (Exception e) {
//			  return String.format("{code:500,msg:\"%s\"}", StringEscapeUtils.escapeJavaScript(e.getMessage()));
//		}
//	}
//    
//    @RequestMapping(value="/countseries",method=RequestMethod.GET)
//   	@ResponseBody
//   	public String countSeries(@RequestParam Integer limit,@RequestParam Double rate){
//   		try {
//   			TreeDataNode dataNode = stockService.countSeries(limit, rate);
//   			return DataNodeUtil.toJson(dataNode);
//   		} catch (Exception e) {
//   			  return String.format("{code:500,msg:\"%s\"}", StringEscapeUtils.escapeJavaScript(e.getMessage()));
//   		}
//   	}
//    
//    @RequestMapping(value="/customer",method=RequestMethod.POST)
//	@ResponseBody
//	public String insertCustomer(@RequestBody(required=true) Map<String,Object> map){
//		try {
//			TreeDataNode dataNode = saleService.insertCustomer((String)map.get("customerName"), (String)map.get("company"), (String)map.get("city"), (String)map.get("area"));
//			return DataNodeUtil.toJson(dataNode);
//		} catch (Exception e) {
//			return String.format("{code:500,msg:\"%s\"}", StringEscapeUtils.escapeJavaScript(e.getMessage()));
//		}
//	}
//    
//    @RequestMapping(value="/customer/{id}",method=RequestMethod.PUT)
//   	@ResponseBody
//   	public String updateCustomer(@PathVariable String id,@RequestBody(required=true) Map<String,Object> map){
//   		try {
//   			TreeDataNode dataNode = saleService.updateCustomer(id,(String)map.get("customerName"), (String)map.get("company"), (String)map.get("city"), (String)map.get("area"));
//   			return DataNodeUtil.toJson(dataNode);
//   		} catch (Exception e) {
//   			return String.format("{code:500,msg:\"%s\"}", StringEscapeUtils.escapeJavaScript(e.getMessage()));
//   		}
//   	}
//    
//    @RequestMapping(value="/customer/{id}",method=RequestMethod.DELETE)
//   	@ResponseBody
//   	public String deleteCustomer(@PathVariable String id){
//   		try {
//   			TreeDataNode dataNode = saleService.deleteCustomer(id);
//   			return DataNodeUtil.toJson(dataNode);
//   		} catch (Exception e) {
//   			return String.format("{code:500,msg:\"%s\"}", StringEscapeUtils.escapeJavaScript(e.getMessage()));
//   		}
//   	}
//    
//    @RequestMapping(value="/customer/{id}",method=RequestMethod.GET)
//   	@ResponseBody
//   	public String getCustomer(@PathVariable String id){
//   		try {
//   			TreeDataNode dataNode = saleService.getCustomer(id);
//   			return DataNodeUtil.toJson(dataNode);
//   		} catch (Exception e) {
//   			return String.format("{code:500,msg:\"%s\"}", StringEscapeUtils.escapeJavaScript(e.getMessage()));
//   		}
//   	}
//    
//    @RequestMapping(value="/customer",method=RequestMethod.GET)
//   	@ResponseBody
//   	public String getCustomers(){
//   		try {
//   			TreeDataNode dataNode = saleService.getCustomers();
//   			return DataNodeUtil.toJson(dataNode);
//   		} catch (Exception e) {
//   			return String.format("{code:500,msg:\"%s\"}", StringEscapeUtils.escapeJavaScript(e.getMessage()));
//   		}
//   	}
//    
//    @RequestMapping(value="/contract",method=RequestMethod.POST)
//	@ResponseBody
//	public String insertContract(@RequestBody(required=true) Map<String,Object> map){
//		try {
//			TreeDataNode dataNode = saleService.insertContract((String)map.get("customerId"), (Double)map.get("amount"), (String)map.get("sellDate"));
//			return DataNodeUtil.toJson(dataNode);
//		} catch (Exception e) {
//			return String.format("{code:500,msg:\"%s\"}", StringEscapeUtils.escapeJavaScript(e.getMessage()));
//		}
//	}
//    
//    @RequestMapping(value="/contract/{id}",method=RequestMethod.PUT)
//   	@ResponseBody
//   	public String updateContract(@PathVariable String id,@RequestBody(required=true) Map<String,Object> map){
//   		try {
//   			TreeDataNode dataNode = saleService.updateContract(id,(String)map.get("customerId"), (Double)map.get("amount"), (String)map.get("sellDate"));
//   			return DataNodeUtil.toJson(dataNode);
//   		} catch (Exception e) {
//   			return String.format("{code:500,msg:\"%s\"}", StringEscapeUtils.escapeJavaScript(e.getMessage()));
//   		}
//   	}
//    
//    @RequestMapping(value="/contract/{id}",method=RequestMethod.DELETE)
//   	@ResponseBody
//   	public String deleteContract(@PathVariable String id){
//   		try {
//   			TreeDataNode dataNode = saleService.deleteContract(id);
//   			return DataNodeUtil.toJson(dataNode);
//   		} catch (Exception e) {
//   			return String.format("{code:500,msg:\"%s\"}", StringEscapeUtils.escapeJavaScript(e.getMessage()));
//   		}
//   	}
//    
//    @RequestMapping(value="/contract/{id}",method=RequestMethod.GET)
//   	@ResponseBody
//   	public String getContract(@PathVariable String id){
//   		try {
//   			TreeDataNode dataNode = saleService.getContract(id);
//   			return DataNodeUtil.toJson(dataNode);
//   		} catch (Exception e) {
//   			return String.format("{code:500,msg:\"%s\"}", StringEscapeUtils.escapeJavaScript(e.getMessage()));
//   		}
//   	}
//    
//    @RequestMapping(value="/contract",method=RequestMethod.GET)
//   	@ResponseBody
//   	public String getContracts(){
//   		try {
//   			TreeDataNode dataNode = saleService.getContracts();
//   			return DataNodeUtil.toJson(dataNode);
//   		} catch (Exception e) {
//   			return String.format("{code:500,msg:\"%s\"}", StringEscapeUtils.escapeJavaScript(e.getMessage()));
//   		}
//   	}
//    
//    @RequestMapping(value="/countvip",method=RequestMethod.GET)
//	@ResponseBody
//	public String countVip(@RequestParam(required=false) String year,@RequestParam Double rate){
//		try {
//			TreeDataNode dataNode = saleService.countVip(year, rate);
//			return DataNodeUtil.toJson(dataNode);
//		} catch (Exception e) {
//			  return String.format("{code:500,msg:\"%s\"}", StringEscapeUtils.escapeJavaScript(e.getMessage()));
//		}
//	}
}
